//
//  SN_ReadListCell.m
//  Shiyun_Newspaper
//
//  Created by Sylar on 2017/6/15.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_ReadListCell.h"

@implementation SN_ReadListCell
{
    UILabel *subNameLabel;
    UIButton *moreBtn;
    SN_Readlist_Model *paperModel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self layoutCell];
    }
    return self;
}

- (void)layoutCell {
    
    subNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 14, 100, 16)];
    [subNameLabel setTextColor:colorFromHex(@"#3b3b3b")];
    [subNameLabel setFont:[UIFont systemFontOfSize:16]];
    [self.contentView addSubview:subNameLabel];
    
    moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreBtn setFrame:CGRectMake(0, 0, SCREENWIDTH, 30)];
    [moreBtn addTarget:self action:@selector(moreAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.contentView addSubview:moreBtn];
    
    UILabel *moreLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH - 15 - 8 - 5-100, 14, 100, 13)];
    moreLabel.textAlignment = NSTextAlignmentRight;
    moreLabel.textColor = colorFromHex(@"#979797");
    moreLabel.font = [UIFont systemFontOfSize:13];
    moreLabel.text = @"更多";
    [self.contentView addSubview:moreLabel];
    
    UIImageView *arrowImg = [[UIImageView alloc]initWithFrame:CGRectMake(moreLabel.right+5, 14, 8, 13)];
    [arrowImg setImage:[UIImage imageNamed:@"more_Record"]];
    [self.contentView addSubview:arrowImg];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(15, 158, SCREENWIDTH, 0.5)];
    [line setBackgroundColor:colorFromHex(@"#dedede")];
    [self.contentView addSubview:line];
}

- (void)creatCell:(SN_Readlist_Model *)model {
    
    paperModel = model;
    moreBtn.tag = model.courseid;
    subNameLabel.text = model.coursename;
    
    NSInteger count = SCREENWIDTH == 320?3:4;
    
    float spacing = count > 1?(SCREENWIDTH - 30 - 75*count)/(count-1):0;
    
    if (count > model.szblist.count) {
        count = model.szblist.count;
    }
    
    for (UIView *view in self.contentView.subviews) {
        if (view.tag > 99) {
            [view removeFromSuperview];
        }
    }
    for (int i =0; i < count; i++) {
        
        SN_NewspaperModel *Model = [model.szblist objectAtIndex:i];
        
        UIButton *paperBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [paperBtn setFrame:CGRectMake(15 + i * spacing + i*75, 46, 75, 100)];
        [paperBtn addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
        paperBtn.tag = i+100;
        paperBtn.imageView.contentMode = UIViewContentModeScaleToFill;
        //[paperBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%ld",IMAGEHOSTURL,Model.pic]] forState:UIControlStateNormal];
        [paperBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%ld",IMAGEHOSTURL,Model.pic]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"newspaper_default"]];
        
        [self.contentView addSubview:paperBtn];
        
    }
    
}

#pragma mark - Delegate

- (IBAction)buttonClick:(id)sender {
    
    UIButton *btn = (UIButton *)sender;
    
    if (self.delegate && [_delegate respondsToSelector:@selector(chooseNewspaper:)]) {
        
        SN_NewspaperModel *Model = [paperModel.szblist objectAtIndex:btn.tag-100];
        [self.delegate chooseNewspaper:Model];
    }
    
}

- (IBAction)moreAction:(id)sender {
    
    UIButton *btn = (UIButton *)sender;
    
    if (self.delegate && [_delegate respondsToSelector:@selector(moreNewspaper:courseName:)]) {
        
        [self.delegate moreNewspaper:btn.tag courseName:paperModel.coursename];
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    
}

@end
